#define LOG_TAG "uart"
#include <COMMON_HEADER/common_header.h>
/*
 * Copyright (c) 2016-2020, Chengdu RST technology co., ltd
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-01-05     shijiabao       the first version
 */

/*宏定义串口名称*/
#define DEBUG_UART "uart4"
#define LTE_UART   "uart1"
#define RS485_UART "uart3"
#define RS232_UART "uart2"

/*定义串口句柄结构体*/
//static rt_device_t debug_serial;
static rt_device_t rs232_serial;
//static rt_device_t lte_serial;
static rt_device_t rs485_serial;

/*定义串口参数配置项*/
struct serial_configure debug_uart_config = RT_SERIAL_CONFIG_DEFAULT;
struct serial_configure lte_uart_config = RT_SERIAL_CONFIG_DEFAULT;
struct serial_configure rs485_uart_config = RT_SERIAL_CONFIG_DEFAULT;
struct serial_configure rs232_uart_config = RT_SERIAL_CONFIG_DEFAULT;

/**
  * @brief  读取片上flash配置，来初始化串口配置结构体参数
  * @param  base_addr   片上flash存储参数配置的起始地址
  * @param  uart_config 需要配置串口的config结构体
  * @param  buff_size   需要配置串口的接收区缓存大小
  * @retval 无
  */
static void uart_config_configure_init(uint32_t base_addr,struct serial_configure uart_config,uint32_t buff_size)
{
    uint8_t flag = 0;
    uint8_t data_buffer[2] ={0};
    /*读取波特率参数并配置*/
    chip_flash_read(base_addr,data_buffer,2);
    flag = data_buffer[1];
    switch(flag)
    {
//        case 0:uart_config.baud_rate = BAUD_RATE_2400;
//            break;
//        case 1:uart_config.baud_rate = BAUD_RATE_4800;
//            break;
//        case 2:uart_config.baud_rate = BAUD_RATE_9600;
//            break;
//        case 3:uart_config.baud_rate = BAUD_RATE_19200;
//            break;
//        case 4:uart_config.baud_rate = BAUD_RATE_38400;
//            break;
//        case 5:uart_config.baud_rate = BAUD_RATE_57600;
//            break;
//        case 6:uart_config.baud_rate = BAUD_RATE_115200;
//            break;
        /*默认115200波特率*/
        default:uart_config.baud_rate = BAUD_RATE_9600;
             break;
    }
    flag = 0;

    /*读取数据位参数并配置*/
    chip_flash_read(base_addr+32,data_buffer,2);
    flag = data_buffer[1];
    switch(flag)
    {
        case 5:uart_config.data_bits = DATA_BITS_5;
            break;
        case 6:uart_config.data_bits = DATA_BITS_6;
            break;
        case 7:uart_config.data_bits = DATA_BITS_7;
            break;
        case 8:uart_config.data_bits = DATA_BITS_8;
            break;
        case 9:uart_config.data_bits = DATA_BITS_9;
            break;
        default:uart_config.data_bits = DATA_BITS_8;
            break;
    }
    flag = 0;

    /*读取停止位参数并配置*/
    chip_flash_read(base_addr+32*2,data_buffer,2);
    flag = data_buffer[1];
    switch(flag)
    {
        case 1:uart_config.stop_bits = STOP_BITS_1;
            break;
        case 2:uart_config.stop_bits = STOP_BITS_1;
            break;
        case 3:uart_config.stop_bits = STOP_BITS_2;
            break;
        default:uart_config.stop_bits = STOP_BITS_1;
            break;
    }
    flag = 0;

    /*读取奇偶校验参数并配置*/
    chip_flash_read(base_addr+32*3,data_buffer,2);
    flag = data_buffer[1];
    switch(flag)
    {
        case 0:uart_config.parity = PARITY_NONE;
            break;
        case 1:uart_config.parity = PARITY_EVEN;
            break;
        case 2:uart_config.parity = PARITY_ODD;
            break;
        default:uart_config.parity = PARITY_NONE;
            break;
    }

    /*配置串口接收区缓存大小*/
    uart_config.bufsz = buff_size;
}

/**
  * @brief  初始化调试串口 默认115200 8-N-1
  * @param  无
  * @retval 无
  */
void debug_uart_init(void)
{
    /* step1：查找串口设备 */
    debug_serial = rt_device_find(DEBUG_UART);
    /* step2：修改串口配置参数 */
    debug_uart_config.baud_rate = BAUD_RATE_115200;      //修改波特率为 115200
    debug_uart_config.data_bits = DATA_BITS_8;           //数据位 8
    debug_uart_config.stop_bits = STOP_BITS_1;           //停止位 1
    debug_uart_config.bufsz     = 2058;                  //修改缓冲区 buff size 为 2048
    debug_uart_config.parity    = PARITY_NONE;           //无奇偶校验位
    /* step3：控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(debug_serial, RT_DEVICE_CTRL_CONFIG, &debug_uart_config);
    //初始化信号量
    debug_uart_semaphore_init();
    //以中断接收模式打开串口设备
    rt_device_open(debug_serial, RT_DEVICE_FLAG_INT_RX);
    /* step5:设置接收回调函数*/
    rt_device_set_rx_indicate(debug_serial, debug_uart_input);
}

/**
  * @brief  初始化rs485串口
  * @param  无
  * @retval 无
  */
void rs485_uart_init(void)
{
    /* step1：查找串口设备 */
    rs485_serial = rt_device_find(RS485_UART);
    /* step2：修改串口配置参数 */
    /*读取片上flash rs485 串口参数配置，并初始化*/
    uart_config_configure_init(CONFIGURE_FLASH_ADDR+RS485_BAUD_RATE_ADDR, rs485_uart_config, 2058);
    rs485_uart_config.baud_rate = 9600;
    /* step3：控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(rs485_serial, RT_DEVICE_CTRL_CONFIG, &rs485_uart_config);
    //初始化串口消息队列
    rs485_uart_messagequeue_init();
    /* step4：打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
    rt_device_open(rs485_serial, RT_DEVICE_FLAG_DMA_RX);
    /* step5:设置接收回调函数*/
    rt_device_set_rx_indicate(rs485_serial, rs485_uart_input);
}

/**
  * @brief  初始化rs232串口
  * @param  无
  * @retval 无
  */
void rs232_uart_init(void)
{
    /* step1：查找串口设备 */
    rs232_serial = rt_device_find(RS232_UART);
    /* step2：修改串口配置参数 */
    /*读取片上flash rs232 串口参数配置，并初始化*/
    uart_config_configure_init(CONFIGURE_FLASH_ADDR+RS232_BAUD_RATE_ADDR,rs232_uart_config,2058);
    /* step3：控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(rs232_serial, RT_DEVICE_CTRL_CONFIG, &rs232_uart_config);
    //初始化串口消息队列
    rs232_uart_messagequeue_init();
    /* step4：打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
    rt_device_open(rs232_serial, RT_DEVICE_FLAG_DMA_RX);
    /* step5:设置接收回调函数*/
    rt_device_set_rx_indicate(rs232_serial, rs232_uart_input);
}


/**
  * @brief  调试串口输出函数
  * @param  data_buffer 输出数据数组
  * @retval 无
  */
void debug_uart_write(char *data_buffer, uint32_t size)
{
    rt_device_write(debug_serial,0,data_buffer,size);
}

/**
  * @brief  rs485串口输出函数
  * @param  data_buffer 输出数据数组
  * @retval 无
  */
void rs485_uart_write(char *data_buffer,uint32_t size)
{
    rs485_pin_on();
    rt_device_write(rs485_serial,0,data_buffer,size);
    rs485_pin_down();
    led2_twinkle();
}

/**
  * @brief  rs232串口输出函数
  * @param  data_buffer 输出数据数组
  * @retval 无
  */
void rs232_uart_write(char *data_buffer,uint32_t size)
{
    rt_device_write(rs232_serial,0,data_buffer,size);
    led2_twinkle();
}


void rs485_debug_print(const char *buff)
{
    rs485_uart_write(buff, sizeof(buff));
}






